/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.service;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.common.refun.CollectionUtil;
import com.jeesite.modules.config.redis.RedisHandle;
import com.jeesite.modules.wx.entity.WxActivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wx.entity.WxBusiness;
import com.jeesite.modules.wx.dao.WxBusinessDao;

/**
 * wx_businessService
 * @author wangjianxin
 * @version 2018-09-28
 */
@Service
@Transactional(readOnly=true)
public class WxBusinessService extends CrudService<WxBusinessDao, WxBusiness> {

	@Autowired
	private WxBusinessDao wxBusinessDao;
	@Autowired
	private WxActivityService wxActivityService;
	@Autowired
	private RedisHandle redisHandle;

	/**
	 * 获取单条数据
	 * @param wxBusiness
	 * @return
	 */
	@Override
	public WxBusiness get(WxBusiness wxBusiness) {
		return super.get(wxBusiness);
	}

	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param wxBusiness
	 * @return
	 */
	@Override
	public Page<WxBusiness> findPage(Page<WxBusiness> page, WxBusiness wxBusiness) {
		return super.findPage(page, wxBusiness);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param wxBusiness
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(WxBusiness wxBusiness) {
		super.save(wxBusiness);
	}

	/**
	 * 更新状态
	 * @param wxBusiness
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(WxBusiness wxBusiness) {
		super.updateStatus(wxBusiness);
	}

	/**
	 * 删除数据
	 * @param wxBusiness
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(WxBusiness wxBusiness) {
		super.delete(wxBusiness);
	}

	public List<WxBusiness> queryFastAll(){
		return wxBusinessDao.queryFastAll();
	}

	public WxBusiness getBase(String id){
		return wxBusinessDao.getBase(id);
	}

	/**
	 * 修改商家信息后更新该商家对应的活动缓存数据
	 * @param wxBusiness
	 */
	public void updateCache(WxBusiness wxBusiness){
		Long groupId = wxBusiness.getGroupId();
		if(null != groupId){
			//根据groupId查询查询活动
			WxActivity wxActivity = new WxActivity();
			wxActivity.setBusinessGroupId(groupId);
			List<WxActivity> wxActivityList = wxActivityService.findList(wxActivity);
			if(CollectionUtil.isNotEmpty(wxActivityList)){
				for(WxActivity temp : wxActivityList){
					String activityId = temp.getId();
					//更新商家logo
					JSONObject cache = (JSONObject) redisHandle.get("businessList:"+activityId);
					if(null == cache){
						logger.info("updateCache,get cache null");
						return;
					}
					JSONArray jsonArrayBusiness = (JSONArray) cache.get("business");
					int len = jsonArrayBusiness.size();
					Boolean addBusiness = true;
					for(int i = 0; i < len;i++){
						JSONObject businessObject = (JSONObject) jsonArrayBusiness.get(i);
						String businessId = (String) businessObject.get("businessId");
						if(businessId.equals(wxBusiness.getId())){
							businessObject.put("businessName",wxBusiness.getName());
							businessObject.put("businessPic",wxBusiness.getPic());
							addBusiness = false;
						}
					}
					//新添加的logo
					if(addBusiness){
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("businessId",wxBusiness.getId());
						jsonObject.put("businessName",wxBusiness.getName());
						jsonObject.put("businessPic",wxBusiness.getPic());
						jsonArrayBusiness.add(jsonObject);
					}

					//更新会员卡
					Boolean mainAddBusiness = true;
					JSONArray jsonArrayMainBusiness = (JSONArray) cache.get("mainBusiness");
					int mlen = jsonArrayMainBusiness.size();
					for(int i = 0; i < mlen;i++){
						JSONObject mainBusinessObject = (JSONObject) jsonArrayMainBusiness.get(i);
						String mainBusinessId = (String) mainBusinessObject.get("mainBusinessId");
						if(mainBusinessId.equals(wxBusiness.getId())){
							mainBusinessObject.put("mainBusinessPic",wxBusiness.getPic());
							mainBusinessObject.put("mainBusinessName",wxBusiness.getName());
							mainBusinessObject.put("mainBusinessAddress",wxBusiness.getAddress());
							mainBusinessObject.put("mainBusinessVipCardImg",wxBusiness.getVipCardImg());
							mainBusinessObject.put("mainBusinessPhone",wxBusiness.getPhone());
							mainAddBusiness = false;
						}
					}
					//新添加的会员卡
					if(mainAddBusiness && jsonArrayMainBusiness.size() > 0){
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("mainBusinessId",wxBusiness.getId());
						jsonObject.put("mainBusinessPic",wxBusiness.getPic());
						jsonObject.put("mainBusinessName",wxBusiness.getName());
						jsonObject.put("mainBusinessAddress",wxBusiness.getAddress());
						jsonObject.put("mainBusinessVipCardImg",wxBusiness.getVipCardImg());
						jsonObject.put("mainBusinessPhone",wxBusiness.getPhone());
						jsonArrayMainBusiness.add(jsonObject);
					}

					redisHandle.set("businessList:"+activityId,cache,60*60*5L);
				}
			}

		}

	}

}